package com.pro.exam.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**   
 * Redis操作接口
 *
 */
public class RedisAPI {
    private static JedisPool pool = null;
    
    /**
     * redis连接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
    public static JedisPool getPool() {
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(500);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(1000 * 100);
            config.setTestOnBorrow(true);
            pool = new JedisPool(config, "120.24.75.6", 6379);
        }
        return pool;
    }
    
    /**
     * 返还到连接池
     * 
     * @param pool 
     * @param redis
     */
    public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.close();
        }
    }
    
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static String get(String key){
        String value = null;
        
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
//            jedis.auth("foobared");
            value = jedis.get(key);
        } catch (Exception e) {
            //释放redis对象
            pool.close();
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
        
        return value;
    }
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static Object getObject(String key){
    	Object value = null;
    	
    	JedisPool pool = null;
    	Jedis jedis = null;
    	try {
    		pool = getPool();
    		jedis = pool.getResource();
//            jedis.auth("foobared");
    		value = jedis.eval(key);
    	} catch (Exception e) {
    		//释放redis对象
    		pool.close();
    		e.printStackTrace();
    	} finally {
    		//返还到连接池
    		returnResource(pool, jedis);
    	}
    	
    	return value;
    }
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static Map<String, Map<String,String>> getAll(){
    	Map<String, Map<String,String>> map = new HashMap<String, Map<String,String>>();
    	
    	JedisPool pool = null;
    	Jedis jedis = null;
    	try {
    		pool = getPool();
    		jedis = pool.getResource();
//            jedis.auth("foobared");
    		Set<String> s = jedis.keys("*");
    		Iterator<String> it = s.iterator();

		 
    		while (it.hasNext()) {
    			String key = (String) it.next();
    			String value = jedis.get(key);
    			JSONObject jasonObject = JSONObject.parseObject(value);
    			Map<String,String> transMap = (Map)jasonObject;
    			map.put(key, transMap);
    			System.out.println(key + value);
    		}
    	} catch (Exception e) {
    		//释放redis对象
    		pool.close();
    		e.printStackTrace();
    	} finally {
    		//返还到连接池
    		returnResource(pool, jedis);
    	}
    	
    	return map;
    }
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static Map<String, String> getAllString(){
    	Map<String, String> map = new HashMap<String, String>();
    	
    	JedisPool pool = null;
    	Jedis jedis = null;
    	try {
    		pool = getPool();
    		jedis = pool.getResource();
//            jedis.auth("foobared");
    		Set<String> s = jedis.keys("*");
    		Iterator<String> it = s.iterator();
    		
    		
    		while (it.hasNext()) {
    			String key = (String) it.next();
    			String value = jedis.get(key);
    			
    			map.put(key, value);
    		}
    	} catch (Exception e) {
    		//释放redis对象
    		pool.close();
    		e.printStackTrace();
    	} finally {
    		//返还到连接池
    		returnResource(pool, jedis);
    	}
    	
    	return map;
    }
    /**
     * set数据
     * 
     * @param key
     * @return
     */
    public static void set(String key, String value){
        
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
//            jedis.auth("foobared");
            jedis.set(key, value);
        } catch (Exception e) {
            //释放redis对象
            pool.close();
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
    }
    
    /**
     * append数据
     * 
     * @param key
     * @return
     */
    public static void append(String key, String value){
        
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
//            jedis.auth("foobared");
            jedis.append(key, value);
        } catch (Exception e) {
            //释放redis对象
            pool.close();
            e.printStackTrace();
        } finally {
            //返还到连接池
            returnResource(pool, jedis);
        }
    }
    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static Long del(String key){
    	Long value = null;
    	
    	JedisPool pool = null;
    	Jedis jedis = null;
    	try {
    		pool = getPool();
    		jedis = pool.getResource();
//            jedis.auth("foobared");
    		value = jedis.del(key);
    	} catch (Exception e) {
    		//释放redis对象
    		pool.close();
    		e.printStackTrace();
    	} finally {
    		//返还到连接池
    		returnResource(pool, jedis);
    	}
    	
    	return value;
    }

    
    public static void main(String[] args) {
		RedisAPI.set("name", "hwn");
		RedisAPI.append("name", "001");
		System.out.println(RedisAPI.get("name"));
	}
}
